function flat1(array, depth = 1, flatArray = []) {
  if (!Array.isArray(array) || typeof depth !== 'number') return
  const len = array.length
  let i = -1

  while (++i < len) {
    if (Array.isArray(array[i]) && depth > 0) {
      flat1(array[i], depth - 1, flatArray)
    } else {
      flatArray.push(array[i])
    }
  }
  return flatArray
}


function flat2(array, depth = 1) {
  if (!Array.isArray(array) || typeof depth !== 'number') return
  return array.reduce((pre, cur) => {
    return (Array.isArray(cur) && depth > 0) ?
      [...pre, ...flat2(cur, depth - 1)] :
      [...pre, cur]
  }, [])
}

function flat3(array) {
  if (!Array.isArray(array)) return
  const res = []
  let target = [...array]

  while (target.length > 0) {
      const pop = target.pop()
      if (Array.isArray(pop)) {
          target.push(...pop)
      } else {
          res.push(pop)
      }
  }
  return res
}